#include "bullettrackitem.h"
#include <QPainter>
#include <QDebug>

const float beginy = 250;
const float endy = 120;
const float endbeginy = 200;
const float increment = 20;
const float Opacityinc = 0.2;

BulletTrackItem::BulletTrackItem() :
    QGraphicsObject(),
    m_btarr(15)
{
    m_timer.setInterval(30);
    connect(&m_timer,SIGNAL(timeout()),this,SLOT(logic()));
    m_timer.start();

    initArr();
}

void BulletTrackItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    for(int i = 0; i < m_btarr.count();++i)
    {
        if(m_btarr[i].bShow)
        {
            painter->save();

            painter->setOpacity(m_btarr[i].fOpacity);
            painter->setPen(QPen(QBrush(Qt::white),m_btarr[i].bCup ? 3 : 6));
            painter->drawLine(m_btarr[i].startPt,m_btarr[i].endPt);

            painter->restore();
        }
    }
}

QRectF BulletTrackItem::boundingRect() const
{
    return QRectF(0,0,640,360);
}

void BulletTrackItem::logic()
{
    for(int i = 0; i < m_btarr.count();++i)
    {
        if(m_btarr[i].bShow)
        {
            if(m_btarr[i].endPt.y() >= endy)
            {
                m_btarr[i].endPt.setY(m_btarr[i].endPt.y() - increment);
            }
            else
            {
                if(m_btarr[i].fOpacity >= 0)
                {
                    m_btarr[i].fOpacity -= Opacityinc;
                }
                else
                {
                    m_btarr[i].bShow = false;
                    m_btarr[i].fOpacity = 1.0;
                    m_btarr[i].endPt.setY(endbeginy);
                }
            }
        }
    }
    update();
}

void BulletTrackItem::initArr()
{
    for(int i = 0; i < m_btarr.count();++i)
    {
        m_btarr[i].bShow = false;
        m_btarr[i].fOpacity = 1.0;
        m_btarr[i].startPt.setY(beginy);
        m_btarr[i].endPt.setY(endbeginy);
        m_btarr[i].bCup = true;
    }
}

void BulletTrackItem::SetPt(float x)
{
    for(int i = 0; i < m_btarr.count();++i)
    {
        if(!m_btarr[i].bShow)
        {
            m_btarr[i].startPt.setX(x);
            m_btarr[i].endPt.setX(x);
            m_btarr[i].bShow = true;
            break;
        }
    }
}
